import 'dart:async';
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nyh_motorcade_app/http/http_address.dart';
import 'package:nyh_motorcade_app/http/http_manager.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/utils/format_utils.dart';
import 'package:nyh_motorcade_app/utils/toast_utils.dart';
import 'package:nyh_motorcade_app/widgets/custom_text_field.dart';

typedef ValueCallback = void Function(String value);

class PaymentConfirmDialog extends StatefulWidget {
  final ValueCallback onValueCallback;
  final String paymentAccountId;
  final String receivedAccountName;
  final String receivedAccountCode;
  final double paymentAmount;

  PaymentConfirmDialog({
    Key? key,
    required this.paymentAccountId,
    required this.receivedAccountName,
    required this.receivedAccountCode,
    required this.paymentAmount,
    required this.onValueCallback,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _PaymentConfirmDialogState();
  }
}

class _PaymentConfirmDialogState extends State<PaymentConfirmDialog> {
  Timer? timer;
  int currentTime = 0;
  String verityCode = "";

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          margin: EdgeInsets.symmetric(vertical: 0, horizontal: 30.w),
          decoration: BoxDecoration(
            color: Colours.color_white,
            borderRadius: BorderRadius.all(
              Radius.circular(10.w),
            ),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                margin: EdgeInsets.only(left: 16.w, top: 20.w, right: 16.w, bottom: 10.w),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Center(
                      child: Text(
                        "确认支付",
                        style: TextStyle(color: Colours.color_text_theme, fontSize: 16.sp, fontWeight: FontWeight.bold),
                      ),
                    ),
                    SizedBox(
                      height: 20.w,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          "账户名称",
                          style: TextStyle(color: Colours.color_9fa0ab, fontSize: 14.sp),
                        ),
                        Text(
                          widget.receivedAccountName,
                          style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: 20.w,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          "公司账号",
                          style: TextStyle(color: Colours.color_9fa0ab, fontSize: 14.sp),
                        ),
                        Text(
                          widget.receivedAccountCode,
                          style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: 20.w,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          "付款金额(元)",
                          style: TextStyle(color: Colours.color_9fa0ab, fontSize: 14.sp),
                        ),
                        Text(
                          FormatUtils.formatMoney(widget.paymentAmount),
                          style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: 10.w,
                    ),
                    CustomTextField(
                      "请输入验证码",
                      textStyle: TextStyle(color: Colours.color_text_theme, fontSize: 16.sp),
                      hintStyle: TextStyle(color: Colours.color_cfcfd5, fontSize: 16.sp),
                      fillColor: Colours.color_white,
                      contentPadding: EdgeInsets.symmetric(vertical: 15.w),
                      keyboardType: TextInputType.number,
                      inputFormatters: [LengthLimitingTextInputFormatter(6), FilteringTextInputFormatter.digitsOnly],
                      suffixIcon: currentTime <= 0
                          ? TextButton(
                              onPressed: () {
                                getVerifyCode();
                              },
                              child: Text(
                                "获取验证码",
                                style: TextStyle(color: Colours.theme, fontSize: 12.sp),
                              ),
                              style: ButtonStyle(
                                backgroundColor: MaterialStateProperty.all(Colours.color_white),
                                shape: MaterialStateProperty.all(
                                  RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(15.w),
                                    side: BorderSide(width: 1.w, color: Colours.theme),
                                  ),
                                ),
                                tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                                minimumSize: MaterialStateProperty.all(Size(95.w, 30.w)),
                                padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                              ),
                            )
                          : Text(
                              "${60 - currentTime}秒后重新发送",
                              style: TextStyle(color: Colours.color_6f7082, fontSize: 14.sp),
                            ),
                      onChanged: (value) {
                        verityCode = value;
                      },
                    ),
                  ],
                ),
              ),
              Divider(
                color: Colours.color_background,
                height: 1.w,
                thickness: 1.w,
              ),
              Row(
                children: [
                  Expanded(
                    child: TextButton(
                      onPressed: () {
                        cancelTimer();
                        Navigator.pop(context);
                      },
                      child: Text(
                        "取消",
                        style: TextStyle(color: Colours.color_6f7082, fontSize: 14.sp),
                      ),
                      style: ButtonStyle(
                        minimumSize: MaterialStateProperty.all(Size(double.infinity, 48.w)),
                        shape: MaterialStateProperty.all(
                          RoundedRectangleBorder(
                            borderRadius: BorderRadius.only(bottomLeft: Radius.circular(10.w)),
                          ),
                        ),
                      ),
                    ),
                  ),
                  Container(
                    color: Colours.color_background,
                    width: 1.w,
                    height: 48.w,
                  ),
                  Expanded(
                    child: TextButton(
                      onPressed: () {
                        if (verityCode.isEmpty) {
                          ToastUtils.toast("请输入验证码");
                          return;
                        }
                        cancelTimer();
                        Navigator.pop(context);
                        widget.onValueCallback(verityCode);
                      },
                      child: Text(
                        "确定",
                        style: TextStyle(color: Colours.color_theme, fontSize: 14.sp),
                      ),
                      style: ButtonStyle(
                        minimumSize: MaterialStateProperty.all(Size(double.infinity, 48.w)),
                        shape: MaterialStateProperty.all(
                          RoundedRectangleBorder(
                            borderRadius: BorderRadius.only(bottomRight: Radius.circular(10.w)),
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  void getVerifyCode() {
    httpManager.post(
      Get.context,
      HttpAddress.GET_COMPANY_SETTLEMENT_VERIFY_CODE,
      {"superId": widget.paymentAccountId},
      isToast: true,
      isLoadingDialog: true,
      onSuccess: (data) {
        ToastUtils.toast("验证码已发送");
        _countdown();
      },
    );
  }

  void _countdown() {
    currentTime = 1;
    if (mounted) {
      setState(() {});
    }
    timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (currentTime < 60) {
        currentTime++;
      } else {
        cancelTimer();
      }
      if (mounted) {
        setState(() {});
      }
    });
  }

  void cancelTimer() {
    if (timer != null) {
      currentTime = 0;
      timer!.cancel();
    }
  }
}
